﻿Imports System.Threading

Public Class Form2
    Dim numero As Integer
    Dim hilo As Thread

    Private Sub txtNumero_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtNumero.KeyPress
        If e.KeyChar = ChrW(13) Then
            numero = txtNumero.Text
            hilo = New Thread(AddressOf factorial)
            hilo.Start()
        End If
    End Sub

    Sub factorial()
        Dim acumulador As Long = 1
        For i As Integer = 1 To numero
            Thread.Sleep(30)
            acumulador *= i
            ProgressBar1.Value = CInt((i * 100) / numero)
        Next

        Thread.Sleep(10)
        lblResultado.Text = acumulador
    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Control.CheckForIllegalCrossThreadCalls = False
    End Sub
End Class